﻿Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class frmBorrowserShop

    Dim Conn As New MySqlConnection()
    Dim com As New MySqlCommand()
    Dim sb As New StringBuilder()
    Dim dr As MySqlDataReader
    Dim dt As DataTable
    Dim tr As MySqlTransaction

    Private Sub frmBorrowserShop_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strConn As String = ConfigurationManager.ConnectionStrings("equ").ConnectionString
        If Conn.State = ConnectionState.Open Then
            Conn.Close()
        End If
        Conn.ConnectionString = strConn
        Conn.Open()
        ShowData()
        FormatData()
    End Sub

    Private Sub ShowData()
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM Shop")
        Dim sql As String = sb.ToString()
        With com
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                dgvShop.DataSource = dt
            Else
                dgvShop.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub

    Private Sub FormatData()
        If dgvShop.RowCount > 0 Then
            With dgvShop
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "ชื่อร้าน"
                .Columns(2).HeaderText = "อีเมล์"
            End With
        End If
    End Sub

    Public ID As String
    Public Name_s As String

    Property ID_s As String
        Get
            Return ID
        End Get
        Set(value As String)
            ID = value
        End Set
    End Property

    Property Name_b As String
        Get
            Return Name_s
        End Get
        Set(value As String)
            Name_s = value
        End Set
    End Property

    Private Sub dgvShop_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvShop.CellMouseUp
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        ID = dgvShop.Rows(e.RowIndex).Cells("id_shop").Value.ToString()
        Name_s = dgvShop.Rows(e.RowIndex).Cells("Name").Value.ToString()
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
        If txtSearch.Text.Trim() = "" Then
            Exit Sub
        End If
        If e.KeyCode = Keys.Enter Then
            sb.Remove(0, sb.Length)
            sb.Append("SELECT * FROM Shop WHERE Name '%" & txtSearch.Text & "%'")
        End If
    End Sub
End Class